package mmi.utils;

import java.util.Vector;

/**
 * 
 * @author louis
 *
 * @param <T>
 */
public class Stats<T extends Number> {
	//		T[] nums; // array of Number or subclass
	Vector<T> nums;


	// Pass the constructor a reference to
	// an array of type Number or subclass.
	public Stats(Vector<T> o) {
		nums = o;
	}

	public double average()
	{
		double sum = sum();
		return sum / nums.size();
	}

	// Determine if two averages are the same.
	// Notice the use of the wildcard.
	boolean sameAvg(Stats<?> ob) {
		if(average() == ob.average()) {
			return true;
		}

		return false;
	}

	public int size()
	{
		return nums.size();
	}


	public double standardDeviation()
	{
		double standardDeviation = 0.0;
		double avg = average();
		Vector<Double> differencesFromMean = new Vector<Double>();

		for(int i=0; i<nums.size(); i++)
		{
			differencesFromMean.add(Math.pow(nums.get(i).doubleValue()-avg, 2));
		}

		Stats<Double> s = new Stats<Double>(differencesFromMean);
		standardDeviation = Math.sqrt(s.sum() / s.size());

		return standardDeviation;
	}

	public double sum()
	{
		double sum = 0.0;
		for(int i=0; i < nums.size(); i++) {
			sum += nums.get(i).doubleValue();
		}
		return sum;
	}
}
